<template>
    <el-dialog center title="一键铺货" :visible.sync="show" width="30%" :before-close="cancel">
        <div class="content">
            <div class="list" v-for="item in listData" :key="item.title.id">
                <div class="title">
                    {{ item.title.name }}
                </div>
                <div class="tag-list" >
                    <el-tag :type="item2.selected?'':'info'"  v-for="item2 in item.list" :key="item2.id" @click="handleSelect(item2)">
                        {{ item2.value }}
                    </el-tag>
                </div>
                
            </div>
            <div class="sale-state">
                <div class="title">
                    产品状态
                </div>
                <el-radio-group v-model="saleState">
                    <el-radio v-for="item in $Options.saleStateList" :label="item.id" :key="item.id">
                        {{ item.name }}
                    </el-radio>
                </el-radio-group>
            </div>
        </div>
        <div slot="footer">
            <div class="footer"> 
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
        </div>
    </el-dialog>
</template>

<script>
    import BusinessServerApi from '../api/goods/businessServer'
    export default {
        props:{
            show:{
                type:Boolean,
                default:false
            },
            serverIds:{
                type:Array,
                default:[],
            },
            businessId:{
                type:Number|String
            }
        },
        watch:{
            show(val){
                if(val){
                    BusinessServerApi.getDistributionInfo({businessId:this.businessId}).then(res=>{
                        let data = res.data
                        let arr = []
                        for(let key in data){
                            let title = null
                            if(key==1)title ="专属"
                            if(key==2)title = "小区"
                            if(key==3)title="公里数"
                            if(key==4)title="地区" 
                            arr.push({
                                title:{id:key,name:title},
                                list:data[key]
                            })
                            
                        }
                        this.listData = arr
                        this.saleState = 2
                    })
                }
            }
        },
        data(){
            return {
                listData:[],
                saleState:2,
            }
        },
        methods: {
            cancel(){
                this.$emit("cancel")
            },
            handleSelect(item){
                if(item.hasOwnProperty('selected')){
                    item.selected = !item.selected
                }else{
                    this.$set(item,'selected',true)
                }
            },
            confirm(){
                let businessVillageIds = []
                this.listData.forEach(item=>{
                    item.list.forEach(item2=>{
                        if(item2.selected){
                            businessVillageIds.push(item2.id)
                        }
                    })
                })
                let params = {
                    serverIds:this.serverIds,
                    businessId:this.businessId,
                    businessVillageIds:businessVillageIds,
                    saleState:this.saleState
                }
                BusinessServerApi.setDistribution(params).then(res=>{
                    this.$emit("confirm")
                })
                
            },
        },
    }
</script>

<style lang="scss" scoped>
    .content{
        .title{
                line-height: 40px;
            }
        .list{
            
            .tag-list{
                display: flex;
                flex-wrap: wrap;
                .el-tag{
                    margin-right: 10px;
                    flex-shrink: 0;
                    cursor:pointer;
                }
            }
            
        }
    }
    .footer{
        text-align: center;
    }
</style>